// 定义一个问诊数据的仓库
// 需要记录类型 医院 科室...
import type { ConsultType } from '@/enums'
import type { PartialConsult } from '@/types/consult'

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useConsultStore = defineStore(
  'cp-consult',

  () => {
    const consult = ref<PartialConsult>({}) // 问诊 全部可选类型

    // 定义函数记录问诊的类型: 找医生 极速问诊  开药问诊
    // store.setType(ConsultType.Fast)
    const setType = (type: ConsultType) => (consult.value.type = type)

    // 定义函数记录医院的类型: 0 1
    // store.setIllnessType(1)
    const setIllnessType = (type: 0 | 1) => (consult.value.illnessType = type)

    // 定义函数记录科室: 科室id
    // store.setDep('123')
    const setDep = (id: string) => (consult.value.depId = id)

    // 定义函数记录病情(文字描述 图片描述 患病时间 是否就诊过)
    // store.setIllness({ illnessDesc: '咳嗽' })
    const setIllness = (
      illness: Pick<
        PartialConsult,
        'illnessDesc' | 'illnessTime' | 'consultFlag' | 'pictures'
      >
    ) => {
      consult.value.illnessDesc = illness.illnessDesc
      consult.value.illnessTime = illness.illnessTime
      consult.value.consultFlag = illness.consultFlag
      consult.value.pictures = illness.pictures
    }

    // 定义函数记录患者
    // store.setPatient('007')
    const setPatient = (id: string) => (consult.value.patientId = id)

    // 清空记录
    // store.clear()
    const clear = () => (consult.value = {})

    // 记录优惠券id
    // store.setCoupon('111')
    const setCoupon = (id?: string) => (consult.value.couponId = id)

    return {
      consult, // 数据本体

      // 7个修改数据的函数
      setType,
      setIllnessType,
      setDep,
      setIllness,
      setPatient,
      clear,
      setCoupon
    }
  },

  {
    persist: true
  }
)
